package com.group.tag;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 登录界面自动填充账号密码
 * 
 * @author zs
 * @version 1.0
 */
public class AutoLoginTagHandler extends SimpleTagSupport {

	@Override
    public void doTag() throws JspException, IOException {
    	PageContext pageContext = (PageContext) getJspContext();
    	HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    	Cookie[] cookies = request.getCookies();
    	String username = "";
    	String password = "";
    	for (Cookie cookie : cookies) {
    		if (cookie.getName().equals("rememberUsername")) {
    			username = cookie.getValue();
    		}
    		if (cookie.getName().equals("rememberPassword")) {
    			password = cookie.getValue();
    		}
    	}
    	pageContext.getOut().write("<div class=\"form-group\">\n" +
                "    <label for=\"loginUsername\">User name</label>\n" +
                "    <input value=\""+username+"\"  type=\"text\" class=\"form-control\" name=\"loginUsername\" aria-describedby=\"emailHelp\" placeholder=\"Enter User name\">\n" +
                "</div>\n" +
                "\n" +
                "<div class=\"form-group\">\n" +
                "    <label for=\"loginPassword\">Password</label>\n" +
                "    <input value=\""+password+"\" type=\"password\" class=\"form-control\" name=\"loginPassword\" placeholder=\"Password\">\n" +
                "</div>");
    	
    }
	
}
